package com.Business;

import java.rmi.RemoteException;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;

import com.DAO.*;
import com.Entity.*;

/**
 * Session Bean implementation class AccountSession
 */
@Stateless
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)
public class GenericSession implements GenericSessionRemote {

	/**
	 * Default constructor.
	 */
	public GenericSession() {
		// TODO Auto-generated constructor stub
	}

	@EJB
	private CompanyUserFacade companyUserFacade;
	@EJB
	private ArticleCategoryFacade articleCategoryFacade;
	@EJB
	private ArticleFacade articleFacade;
	@EJB
	private FaqFacade faqFacade;
	@EJB
	private FeedbackFacade feedbackFacade;

	private CompanyUser tempUser;
	private Article tempArticle;
	private ArticleCategory tempArticleCategory;

	@Override
	public CompanyUser validateLogin(String loginCode, String password) throws RemoteException {
		// TODO Auto-generated method stub
		tempUser = companyUserFacade.findByLoginCode(loginCode);
		if (tempUser != null) {
			if (password.equals(tempUser.getLoginPass())) {
				return tempUser;
			}
		}
		return null;
	}

	@Override
	public boolean updateProfile(CompanyUser companyUser) throws RemoteException {
		// TODO Auto-generated method stub
		tempUser = companyUserFacade.find(companyUser.getId());
		tempUser.setLoginPass(companyUser.getLoginPass());
		tempUser.setFirstName(companyUser.getFirstName());
		tempUser.setLastName(companyUser.getLastName());
		tempUser.setGender(companyUser.getGender());
		tempUser.setDateOfBirth(companyUser.getDateOfBirth());
		tempUser.setEmail(companyUser.getEmail());
		tempUser.setPhoneNumber(companyUser.getPhoneNumber());
		tempUser.setAddress(companyUser.getAddress());
		tempUser.setAvatar(companyUser.getAvatar());
		companyUserFacade.edit(tempUser);
		return true;
	}

	
	
	@Override
	public boolean createArticle(Article article) throws RemoteException {
		// TODO Auto-generated method stub
		int authorID = article.getAuthorID().getId();
		int articleCategoryID = article.getArticleCategoryID().getId();
		tempArticle = new Article(0);
		tempUser = companyUserFacade.find(authorID);
		tempArticleCategory = articleCategoryFacade.find(articleCategoryID);
		tempArticle.setTitle(article.getTitle());
		tempArticle.setHtmlContent(article.getHtmlContent());
		tempArticle.setPublishDate(article.getPublishDate());
		tempArticle.setAuthorID(tempUser);
		tempArticle.setArticleCategoryID(tempArticleCategory);
		articleFacade.create(tempArticle);
		return true;
	}

	@Override
	public List<Article> retrieveArticleList(int userID) throws RemoteException {
		// TODO Auto-generated method stub
		tempUser = companyUserFacade.find(userID);
		return tempUser.getArticleList();
	}

	@Override
	public boolean updateArticle(Article article) throws RemoteException {
		// TODO Auto-generated method stub
		int articleCategoryID = article.getArticleCategoryID().getId();
		tempArticle = articleFacade.find(article.getId());
		tempArticleCategory = articleCategoryFacade.find(articleCategoryID);
		tempArticle.setTitle(article.getTitle());
		tempArticle.setHtmlContent(article.getHtmlContent());
		tempArticle.setPublishDate(article.getPublishDate());
		tempArticle.setArticleCategoryID(tempArticleCategory);
		articleFacade.edit(tempArticle);
		return true;
	}

	@Override
	public boolean deleteArticle(int articleID) throws RemoteException {
		// TODO Auto-generated method stub
		tempArticle = articleFacade.find(articleID);
		articleFacade.remove(tempArticle);
		return true;
	}

	@Override
	public Article readArticle(int articleID) throws RemoteException {
		// TODO Auto-generated method stub
		tempArticle = articleFacade.find(articleID);
		return tempArticle;
	}

	@Override
	public boolean rateArticle(int articleID, int stars) throws RemoteException {
		// TODO Auto-generated method stub
		tempArticle = articleFacade.find(articleID);
		tempArticle.setRatingStars(tempArticle.getRatingStars() + stars);
		tempArticle.setRatingTimes(tempArticle.getRatingTimes() + 1);
		articleFacade.edit(tempArticle);
		return true;
	}

	
	
	@Override
	public List<Faq> retrieveFaqList() throws RemoteException {
		// TODO Auto-generated method stub
		return faqFacade.findAll();
	}

	@Override
	public Faq readFaq(int faqID) throws RemoteException {
		// TODO Auto-generated method stub
		Faq tempFaq = faqFacade.find(faqID);
		return tempFaq;
	}

	@Override
	public boolean sendFeedback(Feedback feedback) throws RemoteException {
		// TODO Auto-generated method stub
		Feedback tempFeedback = feedbackFacade.find(feedback.getId());
		tempFeedback.setCompanyUserID(feedback.getCompanyUserID());
		tempFeedback.setTitle(feedback.getTitle());
		tempFeedback.setFeedbackContent(feedback.getFeedbackContent());
		feedbackFacade.create(tempFeedback);
		return true;
	}

}
